<!DOCTYPE html>
<script src="../../../resources/js-test.js"></script>

<script>

description("This tests the constructor for the PointerEvent DOM class.");

debug("--- tests for intrinsic attributes plus screen & client coordinates ---");

["pointerId", "tiltX", "tiltY", "twist"].forEach(function (attr) {
  debug("-- no init --");
  shouldBeZero("new PointerEvent('eventType')." + attr);

  debug("-- init with valid long values --");
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": 0 })." + attr, 0);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": 123 })." + attr, 123);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": -123 })." + attr, -123);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": 2147483647 })." + attr, 2147483647);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": -2147483648 })." + attr, -2147483648);

  debug("-- init with non-long values --");
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": 18446744073709551615 })." + attr);
  shouldBeEqualToNumber("new PointerEvent('eventType', {" + attr + ": 123.45 })." + attr, 123);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": '123abc' })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": 'dummy' })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": NaN })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": null })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": undefined })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": [] })." + attr);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": [12] })." + attr, 12);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": [12, 34] })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": {} })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": {abc:1} })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": {} })." + attr);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": {valueOf: function () { return 123; }} })." + attr, 123);
});

["width", "height", "pressure", "tangentialPressure", "screenX", "screenY", "clientX", "clientY"].forEach(function (attr) {
  debug("-- no init --");
  var defaultValue = (attr == "width" || attr == "height") ? 1 : 0;
  shouldBeEqualToNumber("new PointerEvent('eventType')." + attr, defaultValue);

  debug("-- init with valid float/double values --");
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": 0 })." + attr, 0);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": 123 })." + attr, 123);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": -123 })." + attr, -123);
  shouldBeCloseTo("new PointerEvent('eventType', { " + attr + ": 123.45 })." + attr, 123.45, 0.00001);
  shouldBeCloseTo("new PointerEvent('eventType', { " + attr + ": -123.45 })." + attr, -123.45, 0.00001);
  shouldBeCloseTo("new PointerEvent('eventType', { " + attr + ": 1.23e5 })." + attr, 123000, 0.00001);
  shouldBeCloseTo("new PointerEvent('eventType', { " + attr + ": -1.2e-3 })." + attr, -0.0012, 0.00001);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": 16777215 })." + attr, 16777215);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": -16777216 })." + attr, -16777216);

  debug("-- init with non-float/double values --");
  shouldThrow("new PointerEvent('eventType', { " + attr + ": '123abc' })." + attr);
  shouldThrow("new PointerEvent('eventType', { " + attr + ": 'dummy' })." + attr);
  shouldThrow("new PointerEvent('eventType', { " + attr + ": NaN })." + attr);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": null })." + attr);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": undefined })." + attr, defaultValue);
  shouldBeZero("new PointerEvent('eventType', { " + attr + ": [] })." + attr);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": [12] })." + attr, 12);
  shouldThrow("new PointerEvent('eventType', { " + attr + ": [12, 34] })." + attr);
  shouldThrow("new PointerEvent('eventType', { " + attr + ": {} })." + attr);
  shouldThrow("new PointerEvent('eventType', { " + attr + ": {abc:1} })." + attr);
  shouldThrow("new PointerEvent('eventType', { " + attr + ": {} })." + attr);
  shouldBeEqualToNumber("new PointerEvent('eventType', { " + attr + ": {valueOf: function () { return 123; }} })." + attr, 123);
});

debug("--- tests for inherited attributes (other than screen & client coordinates) ---");

shouldBeFalse("new PointerEvent('eventType').bubbles");
shouldBeFalse("new PointerEvent('eventType').cancelable");
shouldBeNull("new PointerEvent('eventType').view");
shouldBeZero("new PointerEvent('eventType').detail");
shouldBeFalse("new PointerEvent('eventType').ctrlKey");
shouldBeFalse("new PointerEvent('eventType').shiftKey");
shouldBeFalse("new PointerEvent('eventType').altKey");
shouldBeFalse("new PointerEvent('eventType').metaKey");
shouldBeZero("new PointerEvent('eventType').button");
shouldBeZero("new PointerEvent('eventType').buttons");
shouldBeNull("new PointerEvent('eventType').relatedTarget");

debug("--- tests for coalesced events ---");
var coalescedEvents = [
  new PointerEvent('eventType', { 'pressure' : 1, pointerId : 2 }),
  new PointerEvent('eventType', { 'pressure' : 0.5, pointerId : 2 }),
];
var pe = new PointerEvent('eventType', {'coalescedEvents': coalescedEvents, 'pressure' : 0.1, pointerId : 1 });
shouldBeEqualToNumber("pe.getCoalescedEvents().length", coalescedEvents.length);

shouldBeCloseTo("pe.pressure", 0.1, 0.00001);
shouldBeCloseTo("pe.getCoalescedEvents()[0].pressure", 1, 0.00001);
shouldBeCloseTo("pe.getCoalescedEvents()[1].pressure", 0.5, 0.00001);

shouldBeEqualToNumber("pe.pointerId", 1);
shouldBeEqualToNumber("pe.getCoalescedEvents()[0].pointerId", 2);
shouldBeEqualToNumber("pe.getCoalescedEvents()[1].pointerId", 2);


</script>
